package controller;

import com.smart.domain.User;
import com.smart.infer.userInt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
@Controller
public class loginController {
    @Autowired
    private userInt mapper;

    @RequestMapping("/index.html")
    public String index() {
        return "loginAndRegister";
    }

    @RequestMapping("/login.html")
    public ModelAndView log(HttpServletRequest request)throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        String userid = request.getParameter("userid");
        String password = request.getParameter("password");
        request.getSession().setAttribute("userid",userid);
        ModelAndView modelAndView = new ModelAndView();
        int number=mapper.count(userid);
        if(number>0){
            User user=mapper.log(userid);
            System.out.print(userid);
            System.out.print(password);
            System.out.print(user.getPassword());
            if(user.getPassword().equals(password))
                modelAndView.setViewName("Home");
            else {
                modelAndView.addObject("signal","密码错误请重试！");
                modelAndView.setViewName("loginAndRegister");
            }
        }
        else {
            modelAndView.addObject("signal","用户不存在，请重试或注册");
            modelAndView.setViewName("loginAndRegister");

        }

        return modelAndView;
    }
}
